<?php


namespace App\Http\Controllers\Common;


use App\Http\Controllers\Controller;
use App\Models\Region;
use App\Models\Service;
use App\Models\ServiceType;
use App\Repositories\CarBrandRepository;
use App\Repositories\CarRepository;
use App\Repositories\CompanyRepository;
use App\Repositories\DriverRepository;
use App\Repositories\LicenceRepository;
use App\Repositories\RegionRepository;
use App\Repositories\ServiceRepository;
use App\Transforms\CarTransformer;
use Illuminate\Http\Request;

class ListController extends Controller
{

    /**
     * 车辆品牌列表
     * @param Request $request
     * @param CarBrandRepository $repository
     * @return ListController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getBrand(Request $request, CarBrandRepository $repository)
    {
        $name = (string)$request->get('name', '');
        $list = $repository->getList($name, ['id', 'name']);
        return $this->sendJson($list);
    }


    /**
     * 司机列表
     * @param Request $request
     * @param DriverRepository $repository
     * @return ListController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getDriver(Request $request, DriverRepository $repository)
    {
        $search = (string)$request->get('search_name', '');
        $list = $repository->getList($search, ['id', 'real_name', 'phone']);

        return $this->sendJson($list);
    }


    /**
     * 车辆列表
     * @param Request $request
     * @param CarRepository $repository
     * @return ListController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getCar(Request $request, CarRepository $repository)
    {

        $number = $request->get('number', '');
        $list = $repository->getList($number, ['id', 'number','brand_id','type', 'ownership', 'company_id', 'colour', 'restriction', 'people', 'status']);
        $list = (new CarTransformer())->transformgetcarList($list);

        return $this->sendJson($list);
    }


    /**
     * 公司列表
     * @param CompanyRepository $repository
     * @return ListController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getCompany(CompanyRepository $repository)
    {
        $list = $repository->getListByAdmin(['id', 'name']);

        return $this->sendJson($list);
    }


    /**
     * 驾照类型列表
     * @param LicenceRepository $repository
     * @return ListController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getLicence(LicenceRepository $repository)
    {
        $list = $repository->getList(['id', 'name']);

        return $this->sendJson($list);
    }


    /**
     * 公司业务类型列表
     * @param Request $request
     * @param CompanyRepository $repository
     * @return ListController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getService(Request $request, CompanyRepository $repository)
    {
        $companyId = (int)$request->get('company_id', '');
        $service_id = (int)$request->get('type', '2');
//        $list = $repository->getServiceType($companyId);
        $list = ServiceType::query()->where('service_id',$service_id)->where('status', 1)->get();

        return $this->sendJson($list);
    }


    /**
     * 地区列表
     * @return ListController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function getRegion()
    {
        $treeList = (new RegionRepository())->getTreeList();
        return $this->sendJson($treeList);
    }

}
